package com.yyk.yiliao.ui.home.activity;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.yyk.yiliao.R;
import com.yyk.yiliao.base.BaseActivity;
import com.yyk.yiliao.base.BaseRecyclerAdapter;
import com.yyk.yiliao.base.BaseRecyclerHolder;
import com.yyk.yiliao.listener.ToolbarListenter;
import com.yyk.yiliao.util.Md5Util2;
import com.yyk.yiliao.util.ebs.TwoStringEvent;
import com.yyk.yiliao.util.rx.ApiService;
import com.yyk.yiliao.bean.MymessageVisitlisInfo;
import com.yyk.yiliao.util.rx.RxUtils;
import com.yyk.yiliao.widget.diver.CustomDecoration;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 预约挂号 人员选择
 */
public class PersonnelSelection_Activity extends BaseActivity {


    @BindView(R.id.mMember_tishi)
    TextView mMemberTishi;
    @BindView(R.id.mMember_rv)
    RecyclerView mMemberRv;
    private List<MymessageVisitlisInfo.DataBean> memberList = new ArrayList<>();
    private BaseRecyclerAdapter<MymessageVisitlisInfo.DataBean> adapter;


    @Override
    public int getLayoutId() {
        return R.layout.activity_personnel_selection;
    }

    @Override
    public void initView() {
        initToolbar();

    }

    private void initToolbar() {
        setBackArrow();
        setMainTitleRightText("添加", new ToolbarListenter() {
            @Override
            public void onClickListenter() {
                startAct(AddMember_Activity.class);
            }
        });
        setTitle("人员选择");
    }

    @Override
    protected void onResume() {
        super.onResume();
        initPost();
    }

    @Override
    public void initData() {

        initAdapter();


        //                            adapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
        //                                @Override
        //                                public void onItemClick(RecyclerView parent, View view, int position) {
        //                                    int patient_id = data.get(position).getPatient_id();
        //                                    int status = data.get(position).getStatus();
        //                                    String name = data.get(position).getReal_name();
        //                                    if (status == 1) {
        //
        //                                        EventBus.getDefault().postSticky(new MessageEvent2(name + "",patient_id+""));
        //                                        finish();
        //                                    } else {
        //                                        //                                        intnet.putExtra("patient_id", patient_id + "");
        //                                        //                                        intnet.putExtra("status", status + "");
        //                                        EventBus.getDefault().postSticky(new MessageEvent2(name + "",patient_id+""));
        //                                        finish();
        //
        //                                    }
        //
        //                                }
        //                            });


    }

    private void initPost() {
        initMymessageVisitlis(true);
    }

    /**
     * 人员列表
     *
     * @param flag
     */
    private void initMymessageVisitlis(final boolean flag) {
        Map<String, String> map = new TreeMap<>();
        map.put("user_id", Hawk.get("uid", 0) + "");
        map.put("sign", Md5Util2.createSign(map));
        RxUtils.getInstance().retrofit.create(ApiService.class).postMymessageVisitlis(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<MymessageVisitlisInfo>() {

                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(MymessageVisitlisInfo info) {
                        Logger.e("人员选择" + info.toString());

                        if (info.getCode() == 1) {
                            mMemberRv.setVisibility(View.VISIBLE);
                            mMemberTishi.setVisibility(View.GONE);
                            if (flag) {
                                memberList.clear();
                            }

                            if (info.getData().size() > 0 && info.getData() != null) {
                                memberList.addAll(info.getData());
                            }
                            adapter.notifyDataSetChanged();
                        } else {
                            mMemberRv.setVisibility(View.GONE);
                            mMemberTishi.setVisibility(View.VISIBLE);
                        }
                    }

                });
    }

    private void initAdapter() {
        adapter = new BaseRecyclerAdapter<MymessageVisitlisInfo.DataBean>(mActivity, memberList, R.layout.adapter_item_personnelmessage) {
            @Override
            public void convert(BaseRecyclerHolder holder, final MymessageVisitlisInfo.DataBean data, int position, boolean isScrolling) {
                holder.setText(R.id.b_real_name, data.getReal_name());
                holder.getView(R.id.b_see).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String real_name = data.getReal_name();
                        int patient_id = data.getPatient_id();
                        EventBus.getDefault().post(new TwoStringEvent(real_name, patient_id + ""));
                        finish();
                    }
                });

            }
        };
        mMemberRv.addItemDecoration(new CustomDecoration(mActivity, CustomDecoration.VERTICAL_LIST, R.drawable.divider_eb_1px, 0));
        mMemberRv.setLayoutManager(new LinearLayoutManager(mActivity));
        mMemberRv.setAdapter(adapter);

    }


}
